<?php
namespace App\Admin\Controllers\ApiSys;

use App\Http\Controllers\Controller;
use App\Models\KolManage\KolSku;
use App\Models\Product\ProductSku;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class ProductsController extends Controller{

    public function skus(Request $request)
    {
        $q = $request->get('q');
        return ProductSku::query()
            ->where('psku_code', 'like', $q)
            ->get(['id', DB::raw('psku_code as text')]);
    }

    public function vagueSku(Request $request)
    {
        $q = $request->get('q');
        return ProductSku::query()
            ->where('psku_code', 'like', "%$q%")
            ->where('status', ProductSku::STATUS_YES)
            ->paginate(null, ['id', 'psku_code as text']);
    }

    public function vagueSkuCn(Request $request)
    {
        $q = $request->get('q');
        return ProductSku::query()
            ->where('psku_cn', 'like', "%$q%")
            ->where('status', ProductSku::STATUS_YES)
            ->paginate(null, ['id', 'psku_cn as text']);
    }

    public function skuSupplier(Request $request)
    {
        $q = $request->get('q');
        return ProductSku::query()
            ->where('is_mix',0)
            ->where('psku_code', 'like', "%$q%")
            ->paginate(null, ['id', 'psku_code as text']);
    }

    protected function match(Request $request){
        $id = $request->get('id');
        $pSkuCn = ProductSku::getValueById($id,'psku_cn');
        return $pSkuCn ? json_encode($pSkuCn) : $id;
    }

    public function country(Request $request)
    {
        $q = $request->get('q');
        return DB::table('b_amazon_account_country as aac')
            ->join('b_sys_country as c','c.id','=','aac.country_id')
            ->where('aac.account_id',  $q)
            ->get(['c.id as id', DB::raw('c.country as text')]);
    }


    public function skuList(Request $request){
        $q = $request->get('q');
        return ProductSku::query()
            ->where('classify_id', $q)
            ->where('status', ProductSku::STATUS_YES)
            ->get(['id', DB::raw('psku_cn as text')]);
    }

    /**
     * kol产品
     * @param Request $request
     * @return \Illuminate\Support\Collection
     */
    public function kolSkuList(Request $request){
        $q = $request->get('q');

        return DB::table('k_kol_sku as kol_sku')
            ->leftJoin('k_kol as kol','kol.id','=','kol_sku.kol_id')
            ->leftJoin('product_sku as sku','sku.id','=','kol_sku.psku_id')
            ->where('kol_sku.kol_id', $q)
            ->where('kol_sku.status',  KolSku::STATUS_YES)
            ->get(['sku.id as id', DB::raw('sku.psku_cn as text')]);
    }
}
